/**
 * Created with IntelliJ IDEA.
 * Description:无头单向非循环链表实现
 * User: YCKOKO
 * Date: 2022-11-08
 * Time: 15:42
 */

public class MySingleList {
    static class ListNode{
        public int val;
        public ListNode next;

        public ListNode(int val) {
            this.val = val;
        }
    }
    public ListNode head;

    //打印单链表
    public void display() {
        ListNode cur = this.head;
        while(cur!=null){
            System.out.print(cur.val + " ");
            cur = cur.next;
        }
        System.out.println();
    }

    //获取单链表的长度
    public int size() {
        int count = 0;
        ListNode cur = this.head;
        while(cur != null){
            count++;
            cur = cur.next;
        }
        return count;
    }

    //头插法插入链表元素
    public void addFirst(int data){
        ListNode node = new ListNode(data);
        node.next = this.head;
        this.head = node;
    }
    //尾插法插入链表元素
    public void addLast(int data){
        ListNode node = new ListNode(data);
        ListNode cur = this.head;
        if(this.head == null){
            this.head = node;
        }else{
            while(cur.next != null){
                cur = cur.next;
            }
            cur.next = node;
            node.next = null;
        }
    }

    //任意位置插入链表元素,第一个数据节点为0号下标
    public void addIndex(int index,int data) {
        if(index<0 || index>this.size()){
            System.out.println("输入的位置下标不合法！");
            return;
        }
        if(index == 0) {
            addFirst(data);
            return;
        }
        if(index == this.size()) {
            addLast(data);
            return;
        }
        ListNode cur = findIndex(index);
        ListNode node = new ListNode(data);
        node.next = cur.next;
        cur.next = node;
    }

    //查找第index-1个数据节点
    public ListNode findIndex(int index) {
        ListNode cur = this.head;
        while(index-1 != 0){
            cur = cur.next;
            index--;
        }
        return cur;
    }

    //查找链表中是否包含关键字key的数据节点
    public boolean contains(int key) {
        ListNode cur = this.head;
        while(cur!=null){
            if(cur.val == key){
                return true;
            }
            cur = cur.next;
        }
        return false;
    }
    //删除第一个值为key的数据节点
    public void remove(int key) {
        if(this.head == null){
            System.out.println("想要删除的节点不存在！");
            return;
        }
        if(this.head.val == key){
            this.head = this.head.next;
        }
        ListNode cur = findPrevOfKey(key);
        if(cur == null) {
            System.out.println("想要删除的节点不存在！");
            return;
        }
        ListNode del = cur.next;
        cur.next = del.next;
    }

    //查找想要删除的节点的前一个数据结点
    public ListNode findPrevOfKey(int key) {
        ListNode cur = this.head;
        while(cur.next!=null){
            if(cur.next.val == key){
                return cur;
            }
            cur = cur.next;
        }
        return null;
    }

    //删除所有值为key的数据节点
    public void removeAllKey(int key) {
        if(this.head == null){
            System.out.println("想要删除的节点不存在！");
            return;
        }
        ListNode cur = this.head.next;
        ListNode prev = this.head;
        while(cur != null){
            if(cur.val == key){
                prev.next = cur.next;
                cur = cur.next;
            }else{
                prev = cur;
                cur = cur.next;
            }
        }
        if(this.head.val == key){
            this.head = this.head.next;
        }
    }

    //清空单链表
    public void clear() {
        ListNode cur = this.head;
        while(cur!=null){
            ListNode curNext = cur.next;
            cur.next = null;
            cur = curNext;
        }
        this.head = null;
    }
}
